package com.zzl.admin.junit5;

import com.gitee.yongzhuzl.commonutil.util.rest.RestTemplateUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;

/**
 * description: Junit5测试1
 * @author 朱正磊
 * @date 2021-09-23 11:26:01
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest1 {

    @LocalServerPort
    private Integer port;

    private String baseUrl;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @BeforeEach
    void initialize() {
        this.baseUrl = String.format("http://127.0.0.1:%d", port);
    }

    @Test
    void helloTest1() {
        String result = this.testRestTemplate.getForObject(this.baseUrl + "/hello/jack", String.class);
        // result = hello，jack
        System.out.println("result = " + result);
        Assertions.assertEquals(result, "hello，jack");
        org.assertj.core.api.Assertions.assertThat(result).contains("hello，jack");
        // 测试成功！
        System.out.println("测试成功！");
    }

    @Test
    void helloTest2() {
        ResponseEntity<String> response = restTemplateUtil.get(this.baseUrl + "/hello/jack", String.class);
        // hello，jack
        System.out.println(response.getBody());
        Assertions.assertEquals(response.getBody(), "hello，jack");
        org.assertj.core.api.Assertions.assertThat(response.getBody()).contains("hello，jack");
        // 测试成功！
        System.out.println("测试成功！");
    }

}
